package server;

import dbtools.DBOperator;
import model.Client;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Control {
	private static final Logger logger = Logger.getLogger(Control.class.getName());
	private ExecutorService es = Executors.newCachedThreadPool();
	private server.ServerThreadPool serverThread;
	/**
	 * 启动服务器
	 * @return
	 */
	public void doStartServer() {
		serverThread = new server.ServerThreadPool();
		serverThread.setPort(8888);
//		es.execute(serverThread);
	}
	public void doShutDownServer()
	{
		es.shutdownNow();
	}

	/***
	 * 更新服务器端机器的状态
	 * @param client
	 */
	public void NoticeRunStatus(Client client)
	{
		if(client.getStatus().equalsIgnoreCase("true"))updateDatabase(client.getTaskno(),"1");
		else updateDatabase(client.getTaskno(),"0");
	}
	//客户端通知服务端关闭连接
	public void NoticeClosed(Client client)
	{
		logger.log(Level.INFO, "更新客户端连接数据库状态"+client.getTaskno());
		updateDatabase(client.getTaskno(),"0");
	}
	public void updateDatabase(String taskno,String status)
	{
		String sql="update t_fw_task set fd_stat='"+status+"' where fd_taskno='"+taskno+"'";
		DBOperator.execSql(sql);
	}
	public void updateAll(String status)
	{
		String sql="update t_fw_task set fd_stat='"+status+"'";
		DBOperator.execSql(sql);
	}
}